var ws = require('nodejs-websocket')

const PORT = 5001
let myotherdata = {
  id: 1,
  name: 'hhh',
  test: '我是哈哈'
}
const data = {
  deviceId: 'ZCXK001',
  firstTime: 31433141,
  nibpReview: {
    nd: 65,
    nm: 84,
    ns: 123,
    serialNo: 0
  },
  packageCount: 0,
  serialNo: 50330,
  valueData: {
    bluetooth: 0,
    hr: 60,
    power: 67,
    resp: 255,
    serialNo: 50330,
    spo2: 255,
    temp: 36.7
  },
  waveData: {
    ecg: [
      31731, 31739, 31752, 31763, 31777, 31794, 31808, 31819, 31822, 31825, 31828, 31828, 31831,
      31836, 31842, 31850, 31866, 31880, 31887, 31897, 31905, 31903, 31905, 31925, 32047, 32201,
      32424, 32730, 33027, 33217, 33371, 33523, 33674, 33821, 33971, 34120, 34225, 34334, 34482,
      34624, 34763, 34890, 34910, 34791, 34635, 34417, 34106, 33790, 33473, 33153, 32841, 32540,
      32246, 32054, 31912, 31825, 31823, 31836, 31845, 31852, 31859, 31848, 31733, 31599, 31568,
      31694, 32184, 33212, 34971, 37410, 40664, 44611, 48816, 52464, 54864, 55184, 53001, 48571,
      42609, 35996, 29848, 24995, 21865, 20584, 20921, 22338, 24296, 26286, 27965, 29188, 29989,
      30521, 30887, 31101, 31277, 31535, 31749, 31922, 32185, 32487, 32693, 32839, 32977, 33031,
      33136, 33287, 33437, 33584, 33722, 33765, 33765, 33855, 33907, 33944, 34075, 34212, 34311,
      34542, 34796, 34997, 35272, 35512, 35670, 35908, 36146, 36292, 36423, 36551, 36579, 36554,
      36526, 36503, 36472, 36445, 36423, 36403, 36325, 36063, 35735, 35361, 34894, 34464, 34128,
      33791, 33415, 33075, 32665, 32201, 31756, 31399, 31109, 30917, 30872, 30879, 30894, 30903,
      30911, 30920, 30929, 30934, 30940, 30954, 30965, 30979, 30995, 31009, 31015, 31023, 31032,
      31034, 31045, 31054, 31067, 31076, 31087, 31092, 31093, 31101, 31110, 31126, 31137, 31151,
      31160, 31167, 31177, 31188, 31199, 31213, 31224, 31227, 31240, 31256, 31257, 31270, 31280,
      31284, 31293, 31305, 31315, 31323, 31330, 31335, 31343, 31352, 31369, 31383, 31396, 31412,
      31418, 31426, 31433, 31438, 31444, 31455, 31461, 31471, 31482, 31486, 31488, 31496, 31507,
      31521, 31536, 31554, 31558, 31563, 31569, 31580, 31586, 31593, 31608, 31618, 31624, 31635,
      31649, 31652, 31652, 31657, 31660, 31666, 31672, 31685, 31692, 31705, 31717, 31735, 31741,
      31742, 31752, 31755
    ],
    resp: [
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127
    ],
    serialNo: 19,
    spo2: [
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127
    ]
  }
}

var serve = ws.createServer((conn) => {
  console.log('连接成功')
  conn.send('恭喜你 你已经成功和我建立连接了')
  mywebsocket(conn)
  conn.on('error', (err) => {
    console.log('error', err)
  })
  conn.on('close', () => {
    console.log('连接关闭')
  })
  conn.on('text', (data) => {
    console.log('收到了来自客户端的数据', data)
    conn.send(`我是服务端给你说的东西啦,${data},${myotherdata.name}`)
  })
})
var mywebsocket = function (e) {
  let i = 1
  let timer = setInterval(() => {
    // if (i === 4) {
    //   clearInterval(timer)
    //   timer = null
    // }
    e.send(JSON.stringify(data))
    i++
  }, 1000)
}

serve.listen(PORT, () => {
  console.log('服务器启动成功，监听了端口' + PORT)
})
